package hopital.ui;
import hopital.orm.Administrateur;
import hopital.orm.Comptable;
import hopital.orm.Docteur;
import hopital.orm.Infirmier;
import hopital.orm.Labo;
import hopital.orm.Pharmacien;
import hopital.orm.Reception;
import hopital.orm.Schema;
import hopital.orm.Utilisateur;
import hopital.sql.SqlDatabase;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.sql.ResultSet;
import java.util.Properties;
import javax.swing.JFrame;

public class Login extends javax.swing.JFrame {

    public Login() {
        initComponents();
        this.setLocationRelativeTo(null);
        setVisible(true);
    }
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        _close = new javax.swing.JButton();
        _ok = new javax.swing.JButton();
        _user = new javax.swing.JTextField();
        __user = new javax.swing.JLabel();
        __pass = new javax.swing.JLabel();
        _status = new javax.swing.JLabel();
        _pass = new javax.swing.JPasswordField();
        jButton1 = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setIconImage(new javax.swing.ImageIcon(getClass().getResource("/hopital/icon/group41.png")).getImage());

        _close.setIcon(new javax.swing.ImageIcon(getClass().getResource("/hopital/icon/remove11.png"))); // NOI18N
        _close.setLabel("Fermer");
        _close.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                _closeActionPerformed(evt);
            }
        });

        _ok.setIcon(new javax.swing.ImageIcon(getClass().getResource("/hopital/icon/correct8.png"))); // NOI18N
        _ok.setText("Ok");
        _ok.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                _okActionPerformed(evt);
            }
        });

        _user.setToolTipText("");

        __user.setText("User");

        __pass.setText("Pass");

        _status.setForeground(new java.awt.Color(255, 0, 0));

        jButton1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/hopital/icon/cog2.png"))); // NOI18N
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 39, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addGroup(layout.createSequentialGroup()
                                .addGap(0, 101, Short.MAX_VALUE)
                                .addComponent(_ok, javax.swing.GroupLayout.PREFERRED_SIZE, 103, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(18, 18, 18)
                                .addComponent(_close))
                            .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                                .addComponent(__pass)
                                .addGap(18, 18, 18)
                                .addComponent(_pass))
                            .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                                .addComponent(__user)
                                .addGap(18, 18, 18)
                                .addComponent(_user))
                            .addComponent(_status, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                        .addGap(32, 32, 32))))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addGap(46, 46, 46)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(_user, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(__user))
                .addGap(12, 12, 12)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(__pass)
                    .addComponent(_pass, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addComponent(_status, javax.swing.GroupLayout.DEFAULT_SIZE, 19, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(_close, javax.swing.GroupLayout.PREFERRED_SIZE, 0, Short.MAX_VALUE)
                    .addComponent(_ok, javax.swing.GroupLayout.PREFERRED_SIZE, 24, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jButton1)
                .addContainerGap())
        );

        _close.getAccessibleContext().setAccessibleName("_close");

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void _closeActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event__closeActionPerformed
        this.dispose();
    }//GEN-LAST:event__closeActionPerformed

    private void _okActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event__okActionPerformed
        if (_user.getText().equals("") || _pass.getText().equals("")) {
            _status.setText("");
            return;
        }
        Properties _property = new Properties();
        try {
            _property.load(new FileInputStream(new File("CONFIG.")));
            hopital.sql.SqlDatabase.getDatabase(_property);
            if(!hopital.orm.Schema.exists()){
                Schema schema = new hopital.orm.Schema(); 
            }
        } catch (IOException e) {
            Config config = new Config(_property);
        } catch (Exception ex) {
        }
        
        
        JFrame _frame;
        try {

            ResultSet _rs = SqlDatabase.getDatabase().doQuery("SELECT * FROM v_users WHERE c_email=? AND c_password=?", _user.getText(), _pass.getText());
            if (_rs.next()) {
                Utilisateur user;
                switch (_rs.getString("TYPE")) {
                    case "DOCTEUR":
                        user = new Docteur(_rs);
                        _frame = new hopital.docteur.Interface((Docteur) user);
                        break;
                    case "INFIRMIER":
                        user = new Infirmier(_rs);
                        _frame = new hopital.infermier.Interface((Infirmier) user);
                        break;
                    case "RECEPTION":
                        user = new Reception(_rs);
                        _frame = new hopital.reception.Interface((Reception) user);
                        break;
                    case "COMPTABLE":
                        user = new Comptable(_rs);
                        _frame = new hopital.comptable.Interface((Comptable) user);
                        break;
                    case "ADMIN":
                        user = new Administrateur(_rs);
                        _frame = new hopital.admin.Interface((Administrateur) user);
                        break;
                    case "LABO":
                        user = new Labo(_rs);
                        _frame = new hopital.laboratoire.Interface((Labo) user);
                        break;
                    case "PHARMACIEN":
                        user = new Pharmacien(_rs);
                        _frame = new hopital.pharmacien.Interface((Pharmacien) user);
                        break;
                }
                dispose();
            } else {
                _status.setText("ECHEC DE CONNEXION");
            }
        } catch (Exception e) {
        }


    }//GEN-LAST:event__okActionPerformed

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        Properties _property = new Properties();
        try {
            _property.load(new FileInputStream(new File("CONFIG.")));
            
        } catch (IOException e) {
        }
        Config config = new Config(_property);
    }//GEN-LAST:event_jButton1ActionPerformed


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel __pass;
    private javax.swing.JLabel __user;
    private javax.swing.JButton _close;
    private javax.swing.JButton _ok;
    private javax.swing.JPasswordField _pass;
    private javax.swing.JLabel _status;
    private javax.swing.JTextField _user;
    private javax.swing.JButton jButton1;
    // End of variables declaration//GEN-END:variables
}
